<?php
class Number_model extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
	}
	function getNumber()
	{
		$this->db->select();
		$this->db->where('static',0);
		$this->db->limit(1);
		$data = $this->db->get('number')->result_array();
		$sqlData = array('static'=>1);
		$this->db->where('id',$data[0]['id']);
		$this->db->update('number',$sqlData);
		return $data[0]['number'];
	}
	function getNumberNum()
	{
		$this->db->select();
		$this->db->where('static',0);
		return $this->db->get('number')->num_rows();
	}
	function getOrderDown()
	{
		$this->db->select('addorder.order_id,addorder.post_number,addorder.post_address,order.crea_time,user.user');
		$this->db->join('addorder','addorder.post_number=number.number');
		$this->db->join('order','order.order_id=addorder.order_id');
		$this->db->join('user','user.id=order.for_user');
		$this->db->where('number.static',1);
		$data = $this->db->get('number')->result_array();
		return $data;
	}
	function getForDown()
	{
		$this->db->select();
		$this->db->join('addorder','addorder.post_number=number.number');
		$this->db->where('number.static',1);
		$data = $this->db->get('number')->result_array();
		foreach ($data as $k=>$v)
		{
			$re_data[$k]['post_number'] = $v['post_number'];
			if($v['my_address_type'] == 0)
			{
				$areaData = $this->common_func->getArea(array('province'=>$v['my_province'],'city'=>$v['my_city']));
				$re_data[$k]['my_province'] = $areaData['province'];
				$re_data[$k]['my_city'] = $areaData['city'];
				$re_data[$k]['my_name'] = $v['my_name'];
			}
			if($v['my_address_type'] < 0)
			{
				$re_data[$k]['my_province'] = $v['my_province'];
				$re_data[$k]['my_city'] = $v['my_city'];
				$re_data[$k]['my_name'] = $v['my_name'];
			}
			if($v['my_address_type'] > 0)
			{
				$myAddress = $this->My_address_model->getMyAddressById($v['my_address_type']);
				$areaData = $this->common_func->getArea(array('province'=>$myAddress[0]['province'],'city'=>$myAddress[0]['city']));
				$re_data[$k]['my_province'] = $areaData['province'];
				$re_data[$k]['my_city'] = $areaData['city'];
				$re_data[$k]['my_name'] = $myAddress[0]['name'];
			}
			$re_data[$k]['post_address'] = $v['post_address'];
			$re_data[$k]['post_name'] = $v['post_name'];
			$re_data[$k]['post_tel'] = $v['post_tel'];
		}
		return (!isset($re_data))?array():$re_data;
	}
	function setOk($data)
	{
		$upData = array('static'=>2);
		$this->db->where('static',1);
		foreach ($data as $k=>$v)
		{
			$this->db->or_where('number',$v['post_number']);
		}
		$this->db->update('number',$upData);
	}
	function addNumber($data)
	{
		$this->db->select();
		$data = array_unique($data);
		foreach ($data as $k=>$v)
		{
			$this->db->or_where('number',$v);
		}
		$coData = $this->db->get('number')->result_array();
		if(count($coData) > 0)
		{
			$reData = array(
								'tips_img'=>'img/alert.gif',
								'tips_text'=>'<span style="color: rgb(241, 132, 31);">导入失败 ：导入运单号不唯一.</span><span>导入运单号中 ',
					);
			foreach ($coData as $k=>$v)
			{
				$reData['tips_text'] .= $v['number'].' ';
			}
			$reData['tips_text'] .= '和当前库存运单号冲突，不是唯一的，请返回检查后重新导入。</span>';
		}
		else 
		{
			foreach ($data as $k=>$v)
			{
				$sqlData['number'] = $v;
				$sqlData['static'] = 0;
				$this->db->insert('number',$sqlData);
			}
			$reData = array(
								'tips_img'=>'img/ok.gif',
								'tips_text'=>'<span style="color: rgb(241, 132, 31);">导入成功 ：导入所有运单号成功！！！.</span><span>共导入运单号数量'.count($data).'，当前库存量'.$this->getNumberNum().'。</span>',
					);
		}
		return $reData;
	}
}